#-*- encoding: utf-8 -*-

import numpy as np

A = np.array([[3,2000],
              [4,5000],
              [2,3000],
              [5,8000],
              [1,2000]],dtype = 'float')
#数据归一化
mean = np.mean(A,axis = 0)
norm = A -mean
#数据缩放
scope = np.max(norm,axis = 0) - np.min(norm,axis = 0)
norm = norm / scope

#对协方差矩阵进行奇异值分解
U, S, V = np.linalg.svd(np.dot(norm.T,norm))

#由于需要把二维数据将为一维，因此只提取特征矩阵的第一列来构造Ureduce
U_reduce = U[:,0].reshape(2,1)

#有了主成分特征矩阵，就可以对数据进行降维了
R = np.dot(norm,U_reduce)

'''使用sklearn进行PCA降维运算'''
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler

def std_PCA(**argv):
    scaler = MinMaxScaler()
    pca = PCA(**argv)
    pipeline = Pipeline([('scaler',scaler),('pca',pca)])
    return pipeline

pca = std_PCA(n_components=1)
R2 = pca.fit_transform(A)